﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class frmInDiemTBSV : Form
    {
        SqlConnection connection = new SqlConnection("Data Source = .; Initial Catalog = SM; Integrated Security = True");
       // int i = 0;

        public frmInDiemTBSV()
        {
            InitializeComponent();
            groupControl2.Visible = false;
        }

        DataSet FillDataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.Message);
            }
            return ds;
        }


        private void btnOK_Click(object sender, EventArgs e)
        {

            bool ktra = dxValidationProvider1.Validate();
            if (ktra)
            {
                
                DataSet ds = new DataSet();
                if (lkuMalop.Text != "" && txtNamhoc.Text == "" && txtHocky.Text == "")
                {
                    ds = FillDataset("exec PC_InDBTtheolop N'" + lkuMalop.Text + "'");
                    if (ds.Tables[0].Rows.Count == 0)
                        MessageBox.Show("Không tìm thấy dữ liệu");
                    else
                    {
                        groupControl2.Visible = true;
                        gridControl1.DataSource = ds.Tables[0];
                    }
                }
                else if (lkuMalop.Text != "" &&txtNamhoc.Text != "" && txtHocky.Text == "")
                {
                    ds = FillDataset("exec PC_XetLuuBan N'" + lkuMalop.Text + "', N'" + txtNamhoc.Text + "'");
                    if (ds.Tables[0].Rows.Count == 0)
                        MessageBox.Show("Không tìm thấy dữ liệu");
                    else
                    {
                        groupControl2.Visible = true;
                        gridControl1.DataSource = ds.Tables[0];
                    }
                }
                else
                {
                    ds = FillDataset("exec PC_InDTB N'" + lkuMalop.Text + "', N'%" + txtNamhoc.Text + "%', N'" + txtHocky.Text + "'");
                    if (ds.Tables[0].Rows.Count == 0)
                        MessageBox.Show("Không tìm thấy dữ liệu");
                    else
                    {
                        groupControl2.Visible = true;
                        gridControl1.DataSource = ds.Tables[0];
                    }
                }
               
            }
            else
                MessageBox.Show("Bạn chưa nhập dữ liệu cần thiết", "Quản lý sinh viên", MessageBoxButtons.OK, MessageBoxIcon.None);
       
        }

        private void frmInDiemTBSV_Load(object sender, EventArgs e)
        {
            DataSet ds = FillDataset("Select [MalopID] ,[Tenlop] from [T03_Lop_hoc]");
            lkuMalop.Properties.DataSource = ds.Tables[0];
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            bool ktra = dxValidationProvider1.Validate();
            if (ktra)
            {
                string sql;
                DataSet ds = new DataSet();
                if (radioGroup1.SelectedIndex == 0)
                {
                    if (lkuMalop.Text != "" && txtNamhoc.Text == "" && txtHocky.Text == "")
                    {
                        sql = "exec PC_InDBTTangdantheolop N'" + lkuMalop.Text + "', '" + txtDiemtu.Text + "', '" + txtDiemden.Text + "'";
                    }
                    else if (lkuMalop.Text != "" && txtNamhoc.Text != "" && txtHocky.Text == "")
                        sql = "exec PC_InDTBTangdanTheoNamhoc N'" + lkuMalop.Text + "', N'" + txtNamhoc.Text + "', '" + txtDiemtu.Text + "', '" + txtDiemden.Text + "'";
                    else
                        sql = "exec PC_InDTBTangdanTheoHK N'" + lkuMalop.Text + "', N'%" + txtNamhoc.Text + "%', N'" + txtHocky.Text + "',  N'" + txtDiemtu.Text + "', N'" + txtDiemden.Text + "'";
                }
                else
                {
                    if (lkuMalop.Text != "" && txtNamhoc.Text == "" && txtHocky.Text == "")
                    {
                        sql = "exec PC_InDBTGiamdantheolop N'" + lkuMalop.Text + "', '" + txtDiemtu.Text + "', '" + txtDiemden.Text + "'";
                    }
                    else if (lkuMalop.Text != "" && txtNamhoc.Text != "" && txtHocky.Text == "")
                        sql = "exec PC_InDTBGiamdanTheoNamhoc N'" + lkuMalop.Text + "', N'" + txtNamhoc.Text + "', '" + txtDiemtu.Text + "', '" + txtDiemden.Text + "'";
                    else
                        sql = "exec PC_InDTBgiamdanTheoHK N'" + lkuMalop.Text + "', N'%" + txtNamhoc.Text + "%', N'" + txtHocky.Text + "',  N'" + txtDiemtu.Text + "', N'" + txtDiemden.Text + "'";

                }
                rptDiemTBSV rpt = new rptDiemTBSV();
                rpt.DataSource = FillDataset(sql);
                rpt.BindData(lkuMalop.Text);
                rpt.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
                rpt.ShowPreview();
            }
            else
                MessageBox.Show("Bạn nhập thiếu hoặc bạn nhập sai thông tin", "Quản lý sinh viên", MessageBoxButtons.OK, MessageBoxIcon.None);
       
        }


    }
}
